package org.zht.dormpro.auth.service.impl;

import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.Tree;
import org.zht.dormpro.pub.constant.Enums;
import org.zht.dormpro.auth.model.SysPermission;
import org.zht.dormpro.auth.model.SysRolePermission;
import org.zht.dormpro.auth.service.IMenuService;
import org.zht.dormpro.auth.service.IRoleService;
import org.zht.dormpro.auth.service.ISysPermissionService;
import org.zht.dormpro.pub.util.BuildTree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private ISysPermissionService sysPermissionService;
    @Autowired
    private IRoleService roleService;
    @Override
    public Tree<SysPermission> getMenuTree(boolean b){
        List<Tree<SysPermission>> trees = new ArrayList<>();
        List<SysPermission> sysPermissions = sysPermissionService.listMenu(null);
        if(CollectionUtil.isNotEmpty(sysPermissions)){
            for (SysPermission sysPermission : sysPermissions) {
                if(b&&sysPermission.getType()!=0){
                    continue;
                }
                Tree<SysPermission> tree = new Tree<>();
                tree.setId(sysPermission.getId());
                tree.setParentId(sysPermission.getParentId());
                tree.setText(sysPermission.getName());
                trees.add(tree);
            }
        }
        Tree<SysPermission> tree = BuildTree.buildTreeList(trees);
        return tree;
    }

    @Override
    public SysPermission getSysPermission(Integer parentId) {
        return sysPermissionService.getSysPermission(parentId);
    }


    @Override
    public Tree<SysPermission> getMenuTreeById(Integer roleId) {
        //获取所有的菜单选项
        List<SysPermission> sysPermissions = sysPermissionService.listMenu(null);
        //获取选中用户拥有的菜单
        List<SysRolePermission> permissionByRoleId = roleService.getPermissionByRoleId(roleId, Enums.MRStatus.ROLE);
        List<Integer> temp = null;
        //获得选中的id
        if(CollectionUtil.isNotEmpty(permissionByRoleId)){
            temp = new ArrayList<>();
            for (SysRolePermission sysRolePermission : permissionByRoleId) {
                temp.add(sysRolePermission.getSysPermissionId());
            }
        }
        if(CollectionUtil.isNotEmpty(sysPermissions)){
            //选了儿子就不要再选爸爸了
            for (SysPermission sysPermission : sysPermissions) {
                if (CollectionUtil.isNotEmpty(temp)&&temp.contains(sysPermission.getParentId())) {
                    temp.remove(sysPermission.getParentId());
                }
            }
            List<Tree<SysPermission>> trees= new ArrayList<>();
            for (SysPermission sysPermission : sysPermissions) {
                Tree<SysPermission> tree = new Tree<>();
                Integer id = sysPermission.getId();
                tree.setId(id);
                tree.setParentId(sysPermission.getParentId());
                tree.setText(sysPermission.getName());
                Map<String,Object> state = new HashMap<>();
                state.put("selected",false);
                if(CollectionUtil.isNotEmpty(temp)&&temp.contains(id)){
                    state.put("selected",true);
                }
                tree.setState(state);
                trees.add(tree);
            }
            Tree<SysPermission> tree = BuildTree.buildTreeList(trees);
            return tree;
        }
        return null;
    }
}
